home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / share / automake-1.9 / Automake / Variable.pm < prev    next >
Encoding:
Perl POD Document  |  2005-10-13  |  42.2 KB  |  1,591 lines

  1. # Copyright (C) 2003, 2004, 2005  Free Software Foundation, Inc.
  2.  
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2, or (at your option)
  6. # any later version.
  7.  
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12.  
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. # 02110-1301, USA.
  17.  
  18. package Automake::Variable;
  19. use strict;
  20. use Carp;
  21.  
  22. use Automake::Channels;
  23. use Automake::ChannelDefs;
  24. use Automake::Configure_ac;
  25. use Automake::Item;
  26. use Automake::VarDef;
  27. use Automake::Condition qw (TRUE FALSE);
  28. use Automake::DisjConditions;
  29. use Automake::General 'uniq';
  30. use Automake::Wrap 'makefile_wrap';
  31.  
  32. require Exporter;
  33. use vars '@ISA', '@EXPORT', '@EXPORT_OK';
  34. @ISA = qw/Automake::Item Exporter/;
  35. @EXPORT = qw (err_var msg_var msg_cond_var reject_var
  36.           var rvar vardef rvardef
  37.           variables
  38.           scan_variable_expansions check_variable_expansions
  39.           variable_delete
  40.           variables_dump
  41.           set_seen
  42.           require_variables
  43.           variable_value
  44.           output_variables
  45.           transform_variable_recursively);
  46.  
  47. =head1 NAME
  48.  
  49. Automake::Variable - support for variable definitions
  50.  
  51. =head1 SYNOPSIS
  52.  
  53.   use Automake::Variable;
  54.   use Automake::VarDef;
  55.  
  56.   # Defining a variable.
  57.   Automake::Variable::define($varname, $owner, $type,
  58.                              $cond, $value, $comment,
  59.                              $where, $pretty)
  60.  
  61.   # Looking up a variable.
  62.   my $var = var $varname;
  63.   if ($var)
  64.     {
  65.       ...
  66.     }
  67.  
  68.   # Looking up a variable that is assumed to exist.
  69.   my $var = rvar $varname;
  70.  
  71.   # The list of conditions where $var has been defined.
  72.   # ($var->conditions is an Automake::DisjConditions,
  73.   # $var->conditions->conds is a list of Automake::Condition.)
  74.   my @conds = $var->conditions->conds
  75.  
  76.   # Accessing to the definition in Condition $cond.
  77.   # $def is an Automake::VarDef.
  78.   my $def = $var->def ($cond);
  79.   if ($def)
  80.     {
  81.       ...
  82.     }
  83.  
  84.   # When the conditional definition is assumed to exist, use
  85.   my $def = $var->rdef ($cond);
  86.  
  87.  
  88. =head1 DESCRIPTION
  89.  
  90. This package provides support for Makefile variable definitions.
  91.  
  92. An C<Automake::Variable> is a variable name associated to possibly
  93. many conditional definitions.  These definitions are instances
  94. of C<Automake::VarDef>.
  95.  
  96. Therefore obtaining the value of a variable under a given
  97. condition involves two lookups.  One to look up the variable,
  98. and one to look up the conditional definition:
  99.  
  100.   my $var = var $name;
  101.   if ($var)
  102.     {
  103.       my $def = $var->def ($cond);
  104.       if ($def)
  105.         {
  106.           return $def->value;
  107.         }
  108.       ...
  109.     }
  110.   ...
  111.  
  112. When it is known that the variable and the definition
  113. being looked up exist, the above can be simplified to
  114.  
  115.   return var ($name)->def ($cond)->value; # Do not write this.
  116.  
  117. but is better written
  118.  
  119.   return rvar ($name)->rdef ($cond)->value;
  120.  
  121. or even
  122.  
  123.   return rvardef ($name, $cond)->value;
  124.  
  125. The I<r> variants of the C<var>, C<def>, and C<vardef> methods add an
  126. extra test to ensure that the lookup succeeded, and will diagnose
  127. failures as internal errors (with a message which is much more
  128. informative than Perl's warning about calling a method on a
  129. non-object).
  130.  
  131. =cut
  132.  
  133. my $_VARIABLE_PATTERN = '^[.A-Za-z0-9_@]+' . "\$";
  134.  
  135. # The order in which variables should be output.  (May contain
  136. # duplicates -- only the first occurrence matters.)
  137. my @_var_order;
  138.  
  139. # This keeps track of all variables defined by &_gen_varname.
  140. # $_gen_varname{$base} is a hash for all variable defined with
  141. # prefix `$base'.  Values stored this this hash are the variable names.
  142. # Keys have the form "(COND1)VAL1(COND2)VAL2..." where VAL1 and VAL2
  143. # are the values of the variable for condition COND1 and COND2.
  144. my %_gen_varname = ();
  145.  
  146. # Declare the macros that define known variables, so we can
  147. # hint the user if she try to use one of these variables.
  148.  
  149. # Macros accessible via aclocal.
  150. my %_am_macro_for_var =
  151.   (
  152.    ANSI2KNR => 'AM_C_PROTOTYPES',
  153.    CCAS => 'AM_PROG_AS',
  154.    CCASFLAGS => 'AM_PROG_AS',
  155.    EMACS => 'AM_PATH_LISPDIR',
  156.    GCJ => 'AM_PROG_GCJ',
  157.    LEX => 'AM_PROG_LEX',
  158.    LIBTOOL => 'AC_PROG_LIBTOOL',
  159.    lispdir => 'AM_PATH_LISPDIR',
  160.    pkgpyexecdir => 'AM_PATH_PYTHON',
  161.    pkgpythondir => 'AM_PATH_PYTHON',
  162.    pyexecdir => 'AM_PATH_PYTHON',
  163.    PYTHON => 'AM_PATH_PYTHON',
  164.    pythondir => 'AM_PATH_PYTHON',
  165.    U => 'AM_C_PROTOTYPES',
  166.    );
  167.  
  168. # Macros shipped with Autoconf.
  169. my %_ac_macro_for_var =
  170.   (
  171.    ALLOCA => 'AC_FUNC_ALLOCA',
  172.    CC => 'AC_PROG_CC',
  173.    CFLAGS => 'AC_PROG_CC',
  174.    CXX => 'AC_PROG_CXX',
  175.    CXXFLAGS => 'AC_PROG_CXX',
  176.    F77 => 'AC_PROG_F77',
  177.    F77FLAGS => 'AC_PROG_F77',
  178.    FC => 'AC_PROG_FC',
  179.    FCFLAGS => 'AC_PROG_FC',
  180.    RANLIB => 'AC_PROG_RANLIB',
  181.    YACC => 'AC_PROG_YACC',
  182.    );
  183.  
  184. # The name of the configure.ac file.
  185. my $configure_ac = find_configure_ac;
  186.  
  187. # Variables that can be overriden without complaint from -Woverride
  188. my %_silent_variable_override =
  189.   (AM_MAKEINFOHTMLFLAGS => 1,
  190.    AR => 1,
  191.    ARFLAGS => 1,
  192.    DEJATOOL => 1,
  193.    JAVAC => 1,
  194.    JAVAROOT => 1);
  195.  
  196. # Count of helper variables used to implement conditional '+='.
  197. my $_appendvar;
  198.  
  199. # Each call to C<Automake::Variable::traverse_recursively> gets an
  200. # unique label. This is used to detect recursively defined variables.
  201. my $_traversal = 0;
  202.  
  203.  
  204. =head2 Error reporting functions
  205.  
  206. In these functions, C<$var> can be either a variable name, or
  207. an instance of C<Automake::Variable>.
  208.  
  209. =over 4
  210.  
  211. =item C<err_var ($var, $message, [%options])>
  212.  
  213. Uncategorized errors about variables.
  214.  
  215. =cut
  216.  
  217. sub err_var ($$;%)
  218. {
  219.   msg_var ('error', @_);
  220. }
  221.  
  222. =item C<msg_cond_var ($channel, $cond, $var, $message, [%options])>
  223.  
  224. Messages about conditional variable.
  225.  
  226. =cut
  227.  
  228. sub msg_cond_var ($$$$;%)
  229. {
  230.   my ($channel, $cond, $var, $msg, %opts) = @_;
  231.   my $v = ref ($var) ? $var : rvar ($var);
  232.   msg $channel, $v->rdef ($cond)->location, $msg, %opts;
  233. }
  234.  
  235. =item C<msg_var ($channel, $var, $message, [%options])>
  236.  
  237. Messages about variables.
  238.  
  239. =cut
  240.  
  241. sub msg_var ($$$;%)
  242. {
  243.   my ($channel, $var, $msg, %opts) = @_;
  244.   my $v = ref ($var) ? $var : rvar ($var);
  245.   # Don't know which condition is concerned.  Pick any.
  246.   my $cond = $v->conditions->one_cond;
  247.   msg_cond_var $channel, $cond, $v, $msg, %opts;
  248. }
  249.  
  250. =item C<$bool = reject_var ($varname, $error_msg)>
  251.  
  252. Bail out with C<$error_msg> if a variable with name C<$varname> has
  253. been defined.
  254.  
  255. Return true iff C<$varname> is defined.
  256.  
  257. =cut
  258.  
  259. sub reject_var ($$)
  260. {
  261.   my ($var, $msg) = @_;
  262.   my $v = var ($var);
  263.   if ($v)
  264.     {
  265.       err_var $v, $msg;
  266.       return 1;
  267.     }
  268.   return 0;
  269. }
  270.  
  271. =back
  272.  
  273. =head2 Administrative functions
  274.  
  275. =over 4
  276.  
  277. =item C<Automake::Variable::hook ($varname, $fun)>
  278.  
  279. Declare a function to be called whenever a variable
  280. named C<$varname> is defined or redefined.
  281.  
  282. C<$fun> should take two arguments: C<$type> and C<$value>.
  283. When type is C<''> or <':'>, C<$value> is the value being
  284. assigned to C<$varname>.  When C<$type> is C<'+'>, C<$value>
  285. is the value being appended to  C<$varname>.
  286.  
  287. =cut
  288.  
  289. use vars '%_hooks';
  290. sub hook ($$)
  291. {
  292.   my ($var, $fun) = @_;
  293.   $_hooks{$var} = $fun;
  294. }
  295.  
  296. =item C<variables>
  297.  
  298. Returns the list of all L<Automake::Variable> instances.  (I.e., all
  299. variables defined so far.)
  300.  
  301. =cut
  302.  
  303. use vars '%_variable_dict';
  304. sub variables ()
  305. {
  306.   return values %_variable_dict;
  307. }
  308.  
  309. =item C<Automake::Variable::reset>
  310.  
  311. The I<forget all> function.  Clears all know variables and reset some
  312. other internal data.
  313.  
  314. =cut
  315.  
  316. sub reset ()
  317. {
  318.   %_variable_dict = ();
  319.   $_appendvar = 0;
  320.   @_var_order = ();
  321.   %_gen_varname = ();
  322.   $_traversal = 0;
  323. }
  324.  
  325. =item C<var ($varname)>
  326.  
  327. Return the C<Automake::Variable> object for the variable
  328. named C<$varname> if defined.   Return 0 otherwise.
  329.  
  330. =cut
  331.  
  332. sub var ($)
  333. {
  334.   my ($name) = @_;
  335.   return $_variable_dict{$name} if exists $_variable_dict{$name};
  336.   return 0;
  337. }
  338.  
  339. =item C<vardef ($varname, $cond)>
  340.  
  341. Return the C<Automake::VarDef> object for the variable named
  342. C<$varname> if defined in condition C<$cond>.  Return false
  343. if the condition or the variable does not exist.
  344.  
  345. =cut
  346.  
  347. sub vardef ($$)
  348. {
  349.   my ($name, $cond) = @_;
  350.   my $var = var $name;
  351.   return $var && $var->def ($cond);
  352. }
  353.  
  354. # Create the variable if it does not exist.
  355. # This is used only by other functions in this package.
  356. sub _cvar ($)
  357. {
  358.   my ($name) = @_;
  359.   my $v = var $name;
  360.   return $v if $v;
  361.   return _new Automake::Variable $name;
  362. }
  363.  
  364. =item C<rvar ($varname)>
  365.  
  366. Return the C<Automake::Variable> object for the variable named
  367. C<$varname>.  Abort with an internal error if the variable was not
  368. defined.
  369.  
  370. The I<r> in front of C<var> stands for I<required>.  One
  371. should call C<rvar> to assert the variable's existence.
  372.  
  373. =cut
  374.  
  375. sub rvar ($)
  376. {
  377.   my ($name) = @_;
  378.   my $v = var $name;
  379.   prog_error ("undefined variable $name\n" . &variables_dump)
  380.     unless $v;
  381.   return $v;
  382. }
  383.  
  384. =item C<rvardef ($varname, $cond)>
  385.  
  386. Return the C<Automake::VarDef> object for the variable named
  387. C<$varname> if defined in condition C<$cond>.  Abort with an internal
  388. error if the condition or the variable does not exist.
  389.  
  390. =cut
  391.  
  392. sub rvardef ($$)
  393. {
  394.   my ($name, $cond) = @_;
  395.   return rvar ($name)->rdef ($cond);
  396. }
  397.  
  398. =back
  399.  
  400. =head2 Methods
  401.  
  402. C<Automake::Variable> is a subclass of C<Automake::Item>.  See
  403. that package for inherited methods.
  404.  
  405. Here are the methods specific to the C<Automake::Variable> instances.
  406. Use the C<define> function, described latter, to create such objects.
  407.  
  408. =over 4
  409.  
  410. =cut
  411.  
  412. # Create Automake::Variable objects.  This is used
  413. # only in this file.  Other users should use
  414. # the "define" function.
  415. sub _new ($$)
  416. {
  417.   my ($class, $name) = @_;
  418.   my $self = Automake::Item::new ($class, $name);
  419.   $self->{'scanned'} = 0;
  420.   $self->{'last-append'} = []; # helper variable for last conditional append.
  421.   $_variable_dict{$name} = $self;
  422.   return $self;
  423. }
  424.  
  425. # _check_ambiguous_condition ($SELF, $COND, $WHERE)
  426. # -------------------------------------------------
  427. # Check for an ambiguous conditional.  This is called when a variable
  428. # is being defined conditionally.  If we already know about a
  429. # definition that is true under the same conditions, then we have an
  430. # ambiguity.
  431. sub _check_ambiguous_condition ($$$)
  432. {
  433.   my ($self, $cond, $where) = @_;
  434.   my $var = $self->name;
  435.   my ($message, $ambig_cond) = $self->conditions->ambiguous_p ($var, $cond);
  436.  
  437.   # We allow silent variables to be overridden silently.
  438.   my $def = $self->def ($cond);
  439.   if ($message && !($def && $def->pretty == VAR_SILENT))
  440.     {
  441.       msg 'syntax', $where, "$message ...", partial => 1;
  442.       msg_var ('syntax', $var, "... `$var' previously defined here");
  443.       verb ($self->dump);
  444.     }
  445. }
  446.  
  447. =item C<$bool = $var-E<gt>check_defined_unconditionally ([$parent, $parent_cond])>
  448.  
  449. Warn if the variable is conditionally defined.  C<$parent> is the name
  450. of the parent variable, and C<$parent_cond> the condition of the parent
  451. definition.  These two variables are used to display diagnostics.
  452.  
  453. =cut
  454.  
  455. sub check_defined_unconditionally ($;$$)
  456. {
  457.   my ($self, $parent, $parent_cond) = @_;
  458.  
  459.   if (!$self->conditions->true)
  460.     {
  461.       if ($parent)
  462.     {
  463.       msg_cond_var ('unsupported', $parent_cond, $parent,
  464.             "automake does not support conditional definition of "
  465.             . $self->name . " in $parent");
  466.     }
  467.       else
  468.     {
  469.       msg_var ('unsupported', $self,
  470.            "automake does not support " . $self->name
  471.            . " being defined conditionally");
  472.     }
  473.     }
  474. }
  475.  
  476. =item C<$str = $var-E<gt>output ([@conds])>
  477.  
  478. Format all the definitions of C<$var> if C<@cond> is not specified,
  479. else only that corresponding to C<@cond>.
  480.  
  481. =cut
  482.  
  483. sub output ($@)
  484. {
  485.   my ($self, @conds) = @_;
  486.  
  487.   @conds = $self->conditions->conds
  488.     unless @conds;
  489.  
  490.   my $res = '';
  491.   my $name = $self->name;
  492.  
  493.   foreach my $cond (@conds)
  494.     {
  495.       my $def = $self->def ($cond);
  496.       prog_error ("unknown condition `" . $cond->human . "' for `"
  497.           . $self->name . "'")
  498.     unless $def;
  499.  
  500.       next
  501.     if $def->pretty == VAR_SILENT;
  502.  
  503.       $res .= $def->comment;
  504.  
  505.       my $val = $def->raw_value;
  506.       my $equals = $def->type eq ':' ? ':=' : '=';
  507.       my $str = $cond->subst_string;
  508.  
  509.  
  510.       if ($def->pretty == VAR_ASIS)
  511.     {
  512.       my $output_var = "$name $equals $val";
  513.       $output_var =~ s/^/$str/meg;
  514.       $res .= "$output_var\n";
  515.     }
  516.       elsif ($def->pretty == VAR_PRETTY)
  517.     {
  518.       # Suppress escaped new lines.  &makefile_wrap will
  519.       # add them back, maybe at other places.
  520.       $val =~ s/\\$//mg;
  521.       my $wrap = makefile_wrap ("$str$name $equals", "$str\t",
  522.                     split (' ', $val));
  523.  
  524.       # If the last line of the definition is made only of
  525.       # @substitutions@, append an empty variable to make sure it
  526.       # cannot be substituted as a blank line (that would confuse
  527.       # HP-UX Make).
  528.       $wrap = makefile_wrap ("$str$name $equals", "$str\t",
  529.                  split (' ', $val), '$(am__empty)')
  530.         if $wrap =~ /\n(\s*@\w+@)+\s*$/;
  531.  
  532.       $res .= $wrap;
  533.     }
  534.       else # ($def->pretty == VAR_SORTED)
  535.     {
  536.       # Suppress escaped new lines.  &makefile_wrap will
  537.       # add them back, maybe at other places.
  538.       $val =~ s/\\$//mg;
  539.       $res .= makefile_wrap ("$str$name $equals", "$str\t",
  540.                  sort (split (' ' , $val)));
  541.     }
  542.     }
  543.   return $res;
  544. }
  545.  
  546. =item C<@values = $var-E<gt>value_as_list ($cond, [$parent, $parent_cond])>
  547.  
  548. Get the value of C<$var> as a list, given a specified condition,
  549. without recursing through any subvariables.
  550.  
  551. C<$cond> is the condition of interest.  C<$var> does not need
  552. to be defined for condition C<$cond> exactly, but it needs
  553. to be defined for at most one condition implied by C<$cond>.
  554.  
  555. C<$parent> and C<$parent_cond> designate the name and the condition
  556. of the parent variable, i.e., the variable in which C<$var> is
  557. being expanded.  These are used in diagnostics.
  558.  
  559. For example, if C<A> is defined as "C<foo $(B) bar>" in condition
  560. C<TRUE>, calling C<rvar ('A')->value_as_list (TRUE)> will return
  561. C<("foo", "$(B)", "bar")>.
  562.  
  563. =cut
  564.  
  565. sub value_as_list ($$;$$)
  566. {
  567.   my ($self, $cond, $parent, $parent_cond) = @_;
  568.   my @result;
  569.  
  570.   # Get value for given condition
  571.   my $onceflag;
  572.   foreach my $vcond ($self->conditions->conds)
  573.     {
  574.       if ($vcond->true_when ($cond))
  575.     {
  576.       # If there is more than one definitions of $var matching
  577.       # $cond then we are in trouble: tell the user we need a
  578.       # paddle.  Continue by merging results from all conditions,
  579.       # although it doesn't make much sense.
  580.       $self->check_defined_unconditionally ($parent, $parent_cond)
  581.         if $onceflag;
  582.       $onceflag = 1;
  583.  
  584.       my $val = $self->rdef ($vcond)->value;
  585.       push @result, split (' ', $val);
  586.     }
  587.     }
  588.   return @result;
  589. }
  590.  
  591. =item C<@values = $var-E<gt>value_as_list_recursive ([%options])>
  592.  
  593. Return the contents of C<$var> as a list, split on whitespace.  This
  594. will recursively follow C<$(...)> and C<${...}> inclusions.  It
  595. preserves C<@...@> substitutions.
  596.  
  597. C<%options> is a list of option for C<Variable::traverse_recursively>
  598. (see this method).  The most useful is C<cond_filter>:
  599.  
  600.   $var->value_as_list_recursive (cond_filter => $cond)
  601.  
  602. will return the contents of C<$var> and any subvariable in all
  603. conditions implied by C<$cond>.
  604.  
  605. C<%options> can also carry options specific to C<value_as_list_recursive>.
  606. Presently, the only such option is C<location =E<gt> 1> which instructs
  607. C<value_as_list_recursive> to return a list of C<[$location, @values]> pairs.
  608.  
  609. =cut
  610.  
  611. sub value_as_list_recursive ($;%)
  612. {
  613.   my ($var, %options) = @_;
  614.  
  615.   return $var->traverse_recursively
  616.     (# Construct [$location, $value] pairs if requested.
  617.      sub {
  618.        my ($var, $val, $cond, $full_cond) = @_;
  619.        return [$var->rdef ($cond)->location, $val] if $options{'location'};
  620.        return $val;
  621.      },
  622.      # Collect results.
  623.      sub {
  624.        my ($var, $parent_cond, @allresults) = @_;
  625.        return map { my ($cond, @vals) = @$_; @vals } @allresults;
  626.      },
  627.      %options);
  628. }
  629.  
  630.  
  631. =item C<$bool = $var-E<gt>has_conditional_contents>
  632.  
  633. Return 1 if C<$var> or one of its subvariable was conditionally
  634. defined.  Return 0 otherwise.
  635.  
  636. =cut
  637.  
  638. sub has_conditional_contents ($)
  639. {
  640.   my ($self) = @_;
  641.  
  642.   # Traverse the variable recursively until we
  643.   # find a variable defined conditionally.
  644.   # Use `die' to abort the traversal, and pass it `$full_cond'
  645.   # to we can find easily whether the `eval' block aborted
  646.   # because we found a condition, or for some other error.
  647.   eval
  648.     {
  649.       $self->traverse_recursively
  650.     (sub
  651.      {
  652.        my ($subvar, $val, $cond, $full_cond) = @_;
  653.        die $full_cond if ! $full_cond->true;
  654.        return ();
  655.      },
  656.      sub { return (); });
  657.     };
  658.   if ($@)
  659.     {
  660.       return 1 if ref ($@) && $@->isa ("Automake::Condition");
  661.       # Propagate other errors.
  662.       die;
  663.     }
  664.   return 0;
  665. }
  666.  
  667.  
  668. =item C<$string = $var-E<gt>dump>
  669.  
  670. Return a string describing all we know about C<$var>.
  671. For debugging.
  672.  
  673. =cut
  674.  
  675. sub dump ($)
  676. {
  677.   my ($self) = @_;
  678.  
  679.   my $text = $self->name . ": \n  {\n";
  680.   foreach my $vcond ($self->conditions->conds)
  681.     {
  682.       $text .= "    " . $vcond->human . " => " . $self->rdef ($vcond)->dump;
  683.     }
  684.   $text .= "  }\n";
  685.   return $text;
  686. }
  687.  
  688.  
  689. =back
  690.  
  691. =head2 Utility functions
  692.  
  693. =over 4
  694.  
  695. =item C<@list = scan_variable_expansions ($text)>
  696.  
  697. Return the list of variable names expanded in C<$text>.  Note that
  698. unlike some other functions, C<$text> is not split on spaces before we
  699. check for subvariables.
  700.  
  701. =cut
  702.  
  703. sub scan_variable_expansions ($)
  704. {
  705.   my ($text) = @_;
  706.   my @result = ();
  707.  
  708.   # Strip comments.
  709.   $text =~ s/#.*$//;
  710.  
  711.   # Record each use of ${stuff} or $(stuff) that does not follow a $.
  712.   while ($text =~ /(?<!\$)\$(?:\{([^\}]*)\}|\(([^\)]*)\))/g)
  713.     {
  714.       my $var = $1 || $2;
  715.       # The occurence may look like $(string1[:subst1=[subst2]]) but
  716.       # we want only `string1'.
  717.       $var =~ s/:[^:=]*=[^=]*$//;
  718.       push @result, $var;
  719.     }
  720.  
  721.   return @result;
  722. }
  723.  
  724. =item C<check_variable_expansions ($text, $where)>
  725.  
  726. Check variable expansions in C<$text> and warn about any name that
  727. does not conform to POSIX.  C<$where> is the location of C<$text>
  728. for the error message.
  729.  
  730. =cut
  731.  
  732. sub check_variable_expansions ($$)
  733. {
  734.   my ($text, $where) = @_;
  735.   # Catch expansion of variables whose name does not conform to POSIX.
  736.   foreach my $var (scan_variable_expansions ($text))
  737.     {
  738.       if ($var !~ /$_VARIABLE_PATTERN/o)
  739.     {
  740.       # If the variable name contains a space, it's likely
  741.       # to be a GNU make extension (such as $(addsuffix ...)).
  742.       # Mention this in the diagnostic.
  743.       my $gnuext = "";
  744.       $gnuext = "\n(probably a GNU make extension)" if $var =~ / /;
  745.       msg ('portability', $where,
  746.            "$var: non-POSIX variable name$gnuext");
  747.     }
  748.     }
  749. }
  750.  
  751.  
  752.  
  753. =item C<Automake::Variable::define($varname, $owner, $type, $cond, $value, $comment, $where, $pretty)>
  754.  
  755. Define or append to a new variable.
  756.  
  757. C<$varname>: the name of the variable being defined.
  758.  
  759. C<$owner>: owner of the variable (one of C<VAR_MAKEFILE>,
  760. C<VAR_CONFIGURE>, or C<VAR_AUTOMAKE>, defined by L<Automake::VarDef>).
  761. Variables can be overriden, provided the new owner is not weaker
  762. (C<VAR_AUTOMAKE> < C<VAR_CONFIGURE> < C<VAR_MAKEFILE>).
  763.  
  764. C<$type>: the type of the assignment (C<''> for C<FOO = bar>,
  765. C<':'> for C<FOO := bar>, and C<'+'> for C<'FOO += bar'>).
  766.  
  767. C<$cond>: the C<Condition> in which C<$var> is being defined.
  768.  
  769. C<$value>: the value assigned to C<$var> in condition C<$cond>.
  770.  
  771. C<$comment>: any comment (C<'# bla.'>) associated with the assignment.
  772. Comments from C<+=> assignments stack with comments from the last C<=>
  773. assignment.
  774.  
  775. C<$where>: the C<Location> of the assignment.
  776.  
  777. C<$pretty>: whether C<$value> should be pretty printed (one of
  778. C<VAR_ASIS>, C<VAR_PRETTY>, C<VAR_SILENT>, or C<VAR_SORTED>, defined
  779. by by L<Automake::VarDef>).  C<$pretty> applies only to real
  780. assignments.  I.e., it does not apply to a C<+=> assignment (except
  781. when part of it is being done as a conditional C<=> assignment).
  782.  
  783. This function will all run any hook registered with the C<hook>
  784. function.
  785.  
  786. =cut
  787.  
  788. sub define ($$$$$$$$)
  789. {
  790.   my ($var, $owner, $type, $cond, $value, $comment, $where, $pretty) = @_;
  791.  
  792.   prog_error "$cond is not a reference"
  793.     unless ref $cond;
  794.  
  795.   prog_error "$where is not a reference"
  796.     unless ref $where;
  797.  
  798.   prog_error "pretty argument missing"
  799.     unless defined $pretty && ($pretty == VAR_ASIS
  800.                    || $pretty == VAR_PRETTY
  801.                    || $pretty == VAR_SILENT
  802.                    || $pretty == VAR_SORTED);
  803.  
  804.   error $where, "bad characters in variable name `$var'"
  805.     if $var !~ /$_VARIABLE_PATTERN/o;
  806.  
  807.   # `:='-style assignments are not acknowledged by POSIX.  Moreover it
  808.   # has multiple meanings.  In GNU make or BSD make it means "assign
  809.   # with immediate expansion", while in OSF make it is used for
  810.   # conditional assignments.
  811.   msg ('portability', $where, "`:='-style assignments are not portable")
  812.     if $type eq ':';
  813.  
  814.   check_variable_expansions ($value, $where);
  815.  
  816.   # If there's a comment, make sure it is \n-terminated.
  817.   if ($comment)
  818.     {
  819.       chomp $comment;
  820.       $comment .= "\n";
  821.     }
  822.   else
  823.     {
  824.       $comment = '';
  825.     }
  826.  
  827.   my $self = _cvar $var;
  828.  
  829.   my $def = $self->def ($cond);
  830.   my $new_var = $def ? 0 : 1;
  831.  
  832.   # Additional checks for Automake definitions.
  833.   if ($owner == VAR_AUTOMAKE && ! $new_var)
  834.     {
  835.       # An Automake variable must be consistently defined with the same
  836.       # sign by Automake.
  837.       if ($def->type ne $type && $def->owner == VAR_AUTOMAKE)
  838.     {
  839.       error ($def->location,
  840.          "Automake variable `$var' was set with `"
  841.          . $def->type . "=' here...", partial => 1);
  842.       error ($where, "... and is now set with `$type=' here.");
  843.       prog_error ("Automake variable assignments should be consistently\n"
  844.               . "defined with the same sign.");
  845.     }
  846.  
  847.       # If Automake tries to override a value specified by the user,
  848.       # just don't let it do.
  849.       if ($def->owner != VAR_AUTOMAKE)
  850.     {
  851.       if (! exists $_silent_variable_override{$var})
  852.         {
  853.           my $condmsg = ($cond == TRUE
  854.                  ? '' : (" in condition `" . $cond->human . "'"));
  855.           msg_cond_var ('override', $cond, $var,
  856.                 "user variable `$var' defined here$condmsg...",
  857.                 partial => 1);
  858.           msg ('override', $where,
  859.            "... overrides Automake variable `$var' defined here");
  860.         }
  861.       verb ("refusing to override the user definition of:\n"
  862.         . $self->dump ."with `" . $cond->human . "' => `$value'");
  863.       return;
  864.     }
  865.     }
  866.  
  867.   # Differentiate assignment types.
  868.  
  869.   # 1. append (+=) to a variable defined for current condition
  870.   if ($type eq '+' && ! $new_var)
  871.     {
  872.       $def->append ($value, $comment);
  873.       $self->{'last-append'} = [];
  874.  
  875.       # Only increase owners.  A VAR_CONFIGURE variable augmented in a
  876.       # Makefile.am becomes a VAR_MAKEFILE variable.
  877.       $def->set_owner ($owner, $where->clone)
  878.     if $owner > $def->owner;
  879.     }
  880.   # 2. append (+=) to a variable defined for *another* condition
  881.   elsif ($type eq '+' && ! $self->conditions->false)
  882.     {
  883.       # * Generally, $cond is not TRUE.  For instance:
  884.       #     FOO = foo
  885.       #     if COND
  886.       #       FOO += bar
  887.       #     endif
  888.       #   In this case, we declare an helper variable conditionally,
  889.       #   and append it to FOO:
  890.       #     FOO = foo $(am__append_1)
  891.       #     @COND_TRUE@am__append_1 = bar
  892.       #   Of course if FOO is defined under several conditions, we add
  893.       #   $(am__append_1) to each definitions.
  894.       #
  895.       # * If $cond is TRUE, we don't need the helper variable.  E.g., in
  896.       #     if COND1
  897.       #       FOO = foo1
  898.       #     else
  899.       #       FOO = foo2
  900.       #     endif
  901.       #     FOO += bar
  902.       #   we can add bar directly to all definition of FOO, and output
  903.       #     @COND_TRUE@FOO = foo1 bar
  904.       #     @COND_FALSE@FOO = foo2 bar
  905.  
  906.       my $lastappend = [];
  907.       # Do we need an helper variable?
  908.       if ($cond != TRUE)
  909.         {
  910.       # Can we reuse the helper variable created for the previous
  911.       # append?  (We cannot reuse older helper variables because
  912.       # we must preserve the order of items appended to the
  913.       # variable.)
  914.       my $condstr = $cond->string;
  915.       my $key = "$var:$condstr";
  916.       my ($appendvar, $appendvarcond) = @{$self->{'last-append'}};
  917.       if ($appendvar && $condstr eq $appendvarcond)
  918.         {
  919.           # Yes, let's simply append to it.
  920.           $var = $appendvar;
  921.           $owner = VAR_AUTOMAKE;
  922.           $self = var ($var);
  923.           $def = $self->rdef ($cond);
  924.           $new_var = 0;
  925.         }
  926.       else
  927.         {
  928.           # No, create it.
  929.           my $num = ++$_appendvar;
  930.           my $hvar = "am__append_$num";
  931.           $lastappend = [$hvar, $condstr];
  932.           &define ($hvar, VAR_AUTOMAKE, '+',
  933.                $cond, $value, $comment, $where, $pretty);
  934.  
  935.           # Now HVAR is to be added to VAR.
  936.           $comment = '';
  937.           $value = "\$($hvar)";
  938.         }
  939.     }
  940.  
  941.       # Add VALUE to all definitions of SELF.
  942.       foreach my $vcond ($self->conditions->conds)
  943.         {
  944.       # We have a bit of error detection to do here.
  945.       # This:
  946.       #   if COND1
  947.       #     X = Y
  948.       #   endif
  949.       #   X += Z
  950.       # should be rejected because X is not defined for all conditions
  951.       # where `+=' applies.
  952.       my $undef_cond = $self->not_always_defined_in_cond ($cond);
  953.       if (! $undef_cond->false)
  954.         {
  955.           error ($where,
  956.              "Cannot apply `+=' because `$var' is not defined "
  957.              . "in\nthe following conditions:\n  "
  958.              . join ("\n  ", map { $_->human } $undef_cond->conds)
  959.              . "\nEither define `$var' in these conditions,"
  960.              . " or use\n`+=' in the same conditions as"
  961.              . " the definitions.");
  962.         }
  963.       else
  964.         {
  965.           &define ($var, $owner, '+', $vcond, $value, $comment,
  966.                $where, $pretty);
  967.         }
  968.     }
  969.       $self->{'last-append'} = $lastappend;
  970.     }
  971.   # 3. first assignment (=, :=, or +=)
  972.   else
  973.     {
  974.       # There must be no previous value unless the user is redefining
  975.       # an Automake variable or an AC_SUBST variable for an existing
  976.       # condition.
  977.       _check_ambiguous_condition ($self, $cond, $where)
  978.     unless (!$new_var
  979.         && (($def->owner == VAR_AUTOMAKE && $owner != VAR_AUTOMAKE)
  980.             || $def->owner == VAR_CONFIGURE));
  981.  
  982.       # Never decrease an owner.
  983.       $owner = $def->owner
  984.     if ! $new_var && $owner < $def->owner;
  985.  
  986.       # Assignments to a macro set its location.  We don't adjust
  987.       # locations for `+='.  Ideally I suppose we would associate
  988.       # line numbers with random bits of text.
  989.       $def = new Automake::VarDef ($var, $value, $comment, $where->clone,
  990.                    $type, $owner, $pretty);
  991.       $self->set ($cond, $def);
  992.       push @_var_order, $var;
  993.     }
  994.  
  995.   # Call any defined hook.  This helps to update some internal state
  996.   # *while* parsing the file.  For instance the handling of SUFFIXES
  997.   # requires this (see var_SUFFIXES_trigger).
  998.   &{$_hooks{$var}}($type, $value) if exists $_hooks{$var};
  999. }
  1000.  
  1001. =item C<variable_delete ($varname, [@conds])>
  1002.  
  1003. Forget about C<$varname> under the conditions C<@conds>, or completely
  1004. if C<@conds> is empty.
  1005.  
  1006. =cut
  1007.  
  1008. sub variable_delete ($@)
  1009. {
  1010.   my ($var, @conds) = @_;
  1011.  
  1012.   if (!@conds)
  1013.     {
  1014.       delete $_variable_dict{$var};
  1015.     }
  1016.   else
  1017.     {
  1018.       for my $cond (@conds)
  1019.     {
  1020.       delete $_variable_dict{$var}{'defs'}{$cond};
  1021.     }
  1022.     }
  1023. }
  1024.  
  1025. =item C<$str = variables_dump>
  1026.  
  1027. Return a string describing all we know about all variables.
  1028. For debugging.
  1029.  
  1030. =cut
  1031.  
  1032. sub variables_dump ()
  1033. {
  1034.   my $text = "All variables:\n{\n";
  1035.   foreach my $var (sort { $a->name cmp $b->name } variables)
  1036.     {
  1037.       $text .= $var->dump;
  1038.     }
  1039.   $text .= "}\n";
  1040.   return $text;
  1041. }
  1042.  
  1043.  
  1044. =item C<$var = set_seen ($varname)>
  1045.  
  1046. =item C<$var = $var-E<gt>set_seen>
  1047.  
  1048. Mark all definitions of this variable as examined, if the variable
  1049. exists.  See L<Automake::VarDef::set_seen>.
  1050.  
  1051. Return the C<Variable> object if the variable exists, or 0
  1052. otherwise (i.e., as the C<var> function).
  1053.  
  1054. =cut
  1055.  
  1056. sub set_seen ($)
  1057. {
  1058.   my ($self) = @_;
  1059.   $self = ref $self ? $self : var $self;
  1060.  
  1061.   return 0 unless $self;
  1062.  
  1063.   for my $c ($self->conditions->conds)
  1064.     {
  1065.       $self->rdef ($c)->set_seen;
  1066.     }
  1067.  
  1068.   return $self;
  1069. }
  1070.  
  1071.  
  1072. =item C<$count = require_variables ($where, $reason, $cond, @variables)>
  1073.  
  1074. Make sure that each supplied variable is defined in C<$cond>.
  1075. Otherwise, issue a warning showing C<$reason> (C<$reason> should be
  1076. the reason why these variable are required, for instance C<'option foo
  1077. used'>).  If we know which macro can define this variable, hint the
  1078. user.  Return the number of undefined variables.
  1079.  
  1080. =cut
  1081.  
  1082. sub require_variables ($$$@)
  1083. {
  1084.   my ($where, $reason, $cond, @vars) = @_;
  1085.   my $res = 0;
  1086.   $reason .= ' but ' unless $reason eq '';
  1087.  
  1088.  VARIABLE:
  1089.   foreach my $var (@vars)
  1090.     {
  1091.       # Nothing to do if the variable exists.
  1092.       next VARIABLE
  1093.     if vardef ($var, $cond);
  1094.  
  1095.       my $text = "$reason`$var' is undefined\n";
  1096.       my $v = var $var;
  1097.       if ($v)
  1098.     {
  1099.       my $undef_cond = $v->not_always_defined_in_cond ($cond);
  1100.       next VARIABLE
  1101.         if $undef_cond->false;
  1102.       $text .= ("in the following conditions:\n  "
  1103.             . join ("\n  ", map { $_->human } $undef_cond->conds));
  1104.     }
  1105.  
  1106.       ++$res;
  1107.  
  1108.       if (exists $_am_macro_for_var{$var})
  1109.     {
  1110.       $text .= "\nThe usual way to define `$var' is to add "
  1111.         . "`$_am_macro_for_var{$var}'\nto `$configure_ac' and "
  1112.         . "run `aclocal' and `autoconf' again.";
  1113.     }
  1114.       elsif (exists $_ac_macro_for_var{$var})
  1115.     {
  1116.       $text .= "\nThe usual way to define `$var' is to add "
  1117.         . "`$_ac_macro_for_var{$var}'\nto `$configure_ac' and "
  1118.         . "run `autoconf' again.";
  1119.     }
  1120.  
  1121.       error $where, $text, uniq_scope => US_GLOBAL;
  1122.     }
  1123.   return $res;
  1124. }
  1125.  
  1126. =item C<$count = $var->requires_variables ($reason, @variables)>
  1127.  
  1128. Same as C<require_variables>, but a method of Automake::Variable.
  1129. C<@variables> should be defined in the same conditions as C<$var> is
  1130. defined.
  1131.  
  1132. =cut
  1133.  
  1134. sub requires_variables ($$@)
  1135. {
  1136.   my ($var, $reason, @args) = @_;
  1137.   my $res = 0;
  1138.   for my $cond ($var->conditions->conds)
  1139.     {
  1140.       $res += require_variables ($var->rdef ($cond)->location, $reason,
  1141.                  $cond, @args);
  1142.     }
  1143.   return $res;
  1144. }
  1145.  
  1146.  
  1147. =item C<variable_value ($var)>
  1148.  
  1149. Get the C<TRUE> value of a variable, warn if the variable is
  1150. conditionally defined.  C<$var> can be either a variable name
  1151. or a C<Automake::Variable> instance (this allows to calls sucha
  1152. as C<$var-E<gt>variable_value>).
  1153.  
  1154. =cut
  1155.  
  1156. sub variable_value ($)
  1157. {
  1158.     my ($var) = @_;
  1159.     my $v = ref ($var) ? $var : var ($var);
  1160.     return () unless $v;
  1161.     $v->check_defined_unconditionally;
  1162.     return $v->rdef (TRUE)->value;
  1163. }
  1164.  
  1165. =item C<$str = output_variables>
  1166.  
  1167. Format definitions for all variables.
  1168.  
  1169. =cut
  1170.  
  1171. sub output_variables ()
  1172. {
  1173.   my $res = '';
  1174.   # We output variables it in the same order in which they were
  1175.   # defined (skipping duplicates).
  1176.   my @vars = uniq @_var_order;
  1177.  
  1178.   # Output all the Automake variables.  If the user changed one,
  1179.   # then it is now marked as VAR_CONFIGURE or VAR_MAKEFILE.
  1180.   foreach my $var (@vars)
  1181.     {
  1182.       my $v = rvar $var;
  1183.       foreach my $cond ($v->conditions->conds)
  1184.     {
  1185.       $res .= $v->output ($cond)
  1186.         if $v->rdef ($cond)->owner == VAR_AUTOMAKE;
  1187.     }
  1188.     }
  1189.  
  1190.   # Now dump the user variables that were defined.
  1191.   foreach my $var (@vars)
  1192.     {
  1193.       my $v = rvar $var;
  1194.       foreach my $cond ($v->conditions->conds)
  1195.     {
  1196.       $res .= $v->output ($cond)
  1197.         if $v->rdef ($cond)->owner != VAR_AUTOMAKE;
  1198.     }
  1199.     }
  1200.   return $res;
  1201. }
  1202.  
  1203. =item C<$var-E<gt>traverse_recursively (&fun_item, &fun_collect, [cond_filter =E<gt> $cond_filter], [inner_expand =E<gt> 1], [skip_ac_subst =E<gt> 1])>
  1204.  
  1205. Split the value of the Automake::Variable C<$var> on space, and
  1206. traverse its components recursively.
  1207.  
  1208. If C<$cond_filter> is an C<Automake::Condition>, process any
  1209. conditions which are true when C<$cond_filter> is true.  Otherwise,
  1210. process all conditions.
  1211.  
  1212. We distinguish two kinds of items in the content of C<$var>.
  1213. Terms that look like C<$(foo)> or C<${foo}> are subvariables
  1214. and cause recursion.  Other terms are assumed to be filenames.
  1215.  
  1216. Each time a filename is encountered, C<&fun_item> is called with the
  1217. following arguments:
  1218.  
  1219.   ($var,        -- the Automake::Variable we are currently
  1220.                    traversing
  1221.    $val,        -- the item (i.e., filename) to process
  1222.    $cond,       -- the Condition for the $var definition we are
  1223.                    examinating (ignoring the recursion context)
  1224.    $full_cond)  -- the full Condition, taking into account
  1225.                    conditions inherited from parent variables
  1226.                    during recursion
  1227.  
  1228. If C<inner_expand> is set, variable references occuring in filename
  1229. (as in C<$(BASE).ext>) are expansed before the filename is passed to
  1230. C<&fun_item>.
  1231.  
  1232. If C<skip_ac_subst> is set, Autoconf @substitutions@ will be skipped,
  1233. i.e., C<&fun_item> will never be called for them.
  1234.  
  1235. C<&fun_item> may return a list of items, they will be passed to
  1236. C<&fun_store> later on.  Define C<&fun_item> or @<&fun_store> as
  1237. C<undef> when they serve no purpose.
  1238.  
  1239. Once all items of a variable have been processed, the result (of the
  1240. calls to C<&fun_items>, or of recursive traversals of subvariables)
  1241. are passed to C<&fun_collect>.  C<&fun_collect> receives three
  1242. arguments:
  1243.  
  1244.   ($var,         -- the variable being traversed
  1245.    $parent_cond, -- the Condition inherited from parent
  1246.                     variables during recursion
  1247.    @condlist)    -- a list of [$cond, @results] pairs
  1248.                     where each $cond appear only once, and @result
  1249.                     are all the results for this condition.
  1250.  
  1251. Typically you should do C<$cond->merge ($parent_cond)> to recompute
  1252. the C<$full_cond> associated to C<@result>.  C<&fun_collect> may
  1253. return a list of items, that will be used as the result of
  1254. C<Automake::Variable::traverse_recursively> (the top-level, or its
  1255. recursive calls).
  1256.  
  1257. =cut
  1258.  
  1259. # Contains a stack of `from' and `to' parts of variable
  1260. # substitutions currently in force.
  1261. my @_substfroms;
  1262. my @_substtos;
  1263. sub traverse_recursively ($&&;%)
  1264. {
  1265.   ++$_traversal;
  1266.   @_substfroms = ();
  1267.   @_substtos = ();
  1268.   my ($var, $fun_item, $fun_collect, %options) = @_;
  1269.   my $cond_filter = $options{'cond_filter'};
  1270.   my $inner_expand = $options{'inner_expand'};
  1271.   my $skip_ac_subst = $options{'skip_ac_subst'};
  1272.   return $var->_do_recursive_traversal ($var,
  1273.                     $fun_item, $fun_collect,
  1274.                     $cond_filter, TRUE, $inner_expand,
  1275.                     $skip_ac_subst)
  1276. }
  1277.  
  1278. # The guts of Automake::Variable::traverse_recursively.
  1279. sub _do_recursive_traversal ($$&&$$$$)
  1280. {
  1281.   my ($var, $parent, $fun_item, $fun_collect, $cond_filter, $parent_cond,
  1282.       $inner_expand, $skip_ac_subst) = @_;
  1283.  
  1284.   $var->set_seen;
  1285.  
  1286.   if ($var->{'scanned'} == $_traversal)
  1287.     {
  1288.       err_var $var, "variable `" . $var->name() . "' recursively defined";
  1289.       return ();
  1290.     }
  1291.   $var->{'scanned'} = $_traversal;
  1292.  
  1293.   my @allresults = ();
  1294.   my $cond_once = 0;
  1295.   foreach my $cond ($var->conditions->conds)
  1296.     {
  1297.       if (ref $cond_filter)
  1298.     {
  1299.       # Ignore conditions that don't match $cond_filter.
  1300.       next if ! $cond->true_when ($cond_filter);
  1301.       # If we found out several definitions of $var
  1302.       # match $cond_filter then we are in trouble.
  1303.       # Tell the user we don't support this.
  1304.       $var->check_defined_unconditionally ($parent, $parent_cond)
  1305.         if $cond_once;
  1306.       $cond_once = 1;
  1307.     }
  1308.       my @result = ();
  1309.       my $full_cond = $cond->merge ($parent_cond);
  1310.  
  1311.       my @to_process = $var->value_as_list ($cond, $parent, $parent_cond);
  1312.       while (@to_process)
  1313.     {
  1314.       my $val = shift @to_process;
  1315.       # If $val is a variable (i.e. ${foo} or $(bar), not a filename),
  1316.       # handle the sub variable recursively.
  1317.       # (Backslashes before `}' and `)' within brackets are here to
  1318.       # please Emacs's indentation.)
  1319.       if ($val =~ /^\$\{([^\}]*)\}$/ || $val =~ /^\$\(([^\)]*)\)$/)
  1320.         {
  1321.           my $subvarname = $1;
  1322.  
  1323.           # If the user uses a losing variable name, just ignore it.
  1324.           # This isn't ideal, but people have requested it.
  1325.           next if ($subvarname =~ /\@.*\@/);
  1326.  
  1327.           # See if the variable is actually a substitution reference
  1328.           my ($from, $to);
  1329.               # This handles substitution references like ${foo:.a=.b}.
  1330.           if ($subvarname =~ /^([^:]*):([^=]*)=(.*)$/o)
  1331.         {
  1332.           $subvarname = $1;
  1333.           $to = $3;
  1334.           $from = quotemeta $2;
  1335.         }
  1336.  
  1337.           my $subvar = var ($subvarname);
  1338.           # Don't recurse into undefined variables.
  1339.           next unless $subvar;
  1340.  
  1341.           push @_substfroms, $from;
  1342.           push @_substtos, $to;
  1343.  
  1344.           my @res = $subvar->_do_recursive_traversal ($parent,
  1345.                               $fun_item,
  1346.                               $fun_collect,
  1347.                               $cond_filter,
  1348.                               $full_cond,
  1349.                               $inner_expand,
  1350.                               $skip_ac_subst);
  1351.           push (@result, @res);
  1352.  
  1353.           pop @_substfroms;
  1354.           pop @_substtos;
  1355.  
  1356.           next;
  1357.         }
  1358.       # Try to expand variable references inside filenames such as
  1359.       # `$(NAME).txt'.  We do not handle `:.foo=.bar'
  1360.       # substitutions, but it would make little sense to use this
  1361.       # here anyway.
  1362.       elsif ($inner_expand
  1363.          && ($val =~ /\$\{([^\}]*)\}/ || $val =~ /\$\(([^\)]*)\)/))
  1364.         {
  1365.           my $subvarname = $1;
  1366.           my $subvar = var $subvarname;
  1367.           if ($subvar)
  1368.         {
  1369.           # Replace the reference by its value, and reschedule
  1370.           # for expansion.
  1371.           foreach my $c ($subvar->conditions->conds)
  1372.             {
  1373.               if (ref $cond_filter)
  1374.             {
  1375.               # Ignore conditions that don't match $cond_filter.
  1376.               next if ! $c->true_when ($cond_filter);
  1377.               # If we found out several definitions of $var
  1378.               # match $cond_filter then we are in trouble.
  1379.               # Tell the user we don't support this.
  1380.               $subvar->check_defined_unconditionally ($var,
  1381.                                   $full_cond)
  1382.                 if $cond_once;
  1383.               $cond_once = 1;
  1384.             }
  1385.               my $subval = $subvar->rdef ($c)->value;
  1386.               $val =~ s/\$\{$subvarname\}/$subval/g;
  1387.               $val =~ s/\$\($subvarname\)/$subval/g;
  1388.               unshift @to_process, split (' ', $val);
  1389.             }
  1390.           next;
  1391.         }
  1392.           # We do not know any variable with this name.  Fall through
  1393.           # to filename processing.
  1394.         }
  1395.       elsif ($skip_ac_subst && $val =~ /^\@.+\@$/)
  1396.         {
  1397.           next;
  1398.         }
  1399.  
  1400.       if ($fun_item) # $var is a filename we must process
  1401.         {
  1402.           my $substnum=$#_substfroms;
  1403.           while ($substnum >= 0)
  1404.         {
  1405.           $val =~ s/$_substfroms[$substnum]$/$_substtos[$substnum]/
  1406.             if defined $_substfroms[$substnum];
  1407.           $substnum -= 1;
  1408.         }
  1409.  
  1410.           # Make sure you update the doc of
  1411.           # Automake::Variable::traverse_recursively
  1412.           # if you change the prototype of &fun_item.
  1413.           my @transformed = &$fun_item ($var, $val, $cond, $full_cond);
  1414.           push (@result, @transformed);
  1415.         }
  1416.     }
  1417.       push (@allresults, [$cond, @result]) if @result;
  1418.     }
  1419.  
  1420.   # We only care about _recursive_ variable definitions.  The user
  1421.   # is free to use the same variable several times in the same definition.
  1422.   $var->{'scanned'} = -1;
  1423.  
  1424.   return ()
  1425.     unless $fun_collect;
  1426.   # Make sure you update the doc of Automake::Variable::traverse_recursively
  1427.   # if you change the prototype of &fun_collect.
  1428.   return &$fun_collect ($var, $parent_cond, @allresults);
  1429. }
  1430.  
  1431. # $VARNAME
  1432. # _gen_varname ($BASE, @DEFINITIONS)
  1433. # ---------------------------------
  1434. # Return a variable name starting with $BASE, that will be
  1435. # used to store definitions @DEFINITIONS.
  1436. # @DEFINITIONS is a list of pair [$COND, @OBJECTS].
  1437. #
  1438. # If we already have a $BASE-variable containing @DEFINITIONS, reuse it.
  1439. # This way, we avoid combinatorial explosion of the generated
  1440. # variables.  Especially, in a Makefile such as:
  1441. #
  1442. # | if FOO1
  1443. # | A1=1
  1444. # | endif
  1445. # |
  1446. # | if FOO2
  1447. # | A2=2
  1448. # | endif
  1449. # |
  1450. # | ...
  1451. # |
  1452. # | if FOON
  1453. # | AN=N
  1454. # | endif
  1455. # |
  1456. # | B=$(A1) $(A2) ... $(AN)
  1457. # |
  1458. # | c_SOURCES=$(B)
  1459. # | d_SOURCES=$(B)
  1460. #
  1461. # The generated c_OBJECTS and d_OBJECTS will share the same variable
  1462. # definitions.
  1463. #
  1464. # This setup can be the case of a testsuite containing lots (>100) of
  1465. # small C programs, all testing the same set of source files.
  1466. sub _gen_varname ($@)
  1467. {
  1468.   my $base = shift;
  1469.   my $key = '';
  1470.   foreach my $pair (@_)
  1471.     {
  1472.       my ($cond, @values) = @$pair;
  1473.       $key .= "($cond)@values";
  1474.     }
  1475.  
  1476.   return $_gen_varname{$base}{$key} if exists $_gen_varname{$base}{$key};
  1477.  
  1478.   my $num = 1 + keys (%{$_gen_varname{$base}});
  1479.   my $name = "${base}_${num}";
  1480.   $_gen_varname{$base}{$key} = $name;
  1481.   return $name;
  1482. }
  1483.  
  1484. =item C<$resvar = transform_variable_recursively ($var, $resvar, $base, $nodefine, $where, &fun_item, [%options])>
  1485.  
  1486. =item C<$resvar = $var-E<gt>transform_variable_recursively ($resvar, $base, $nodefine, $where, &fun_item, [%options])>
  1487.  
  1488. Traverse C<$var> recursively, and create a C<$resvar> variable in
  1489. which each filename in C<$var> have been transformed using
  1490. C<&fun_item>.  (C<$var> may be a variable name in the first syntax.
  1491. It must be an C<Automake::Variable> otherwise.)
  1492.  
  1493. Helper variables (corresponding to sub-variables of C<$var>) are
  1494. created as needed, using C<$base> as prefix.
  1495.  
  1496. Arguments are:
  1497.   $var       source variable to traverse
  1498.   $resvar    resulting variable to define
  1499.   $base      prefix to use when naming subvariables of $resvar
  1500.   $nodefine  if true, traverse $var but do not define any variable
  1501.              (this assumes &fun_item has some useful side-effect)
  1502.   $where     context into which variable definitions are done
  1503.   &fun_item  a transformation function -- see the documentation
  1504.              of &fun_item in Automake::Variable::traverse_recursively.
  1505.  
  1506. This returns the string C<"\$($RESVAR)">.
  1507.  
  1508. C<%options> is a list of options to pass to
  1509. C<Variable::traverse_recursively> (see this method).
  1510.  
  1511. =cut
  1512.  
  1513. sub transform_variable_recursively ($$$$$&;%)
  1514. {
  1515.   my ($var, $resvar, $base, $nodefine, $where, $fun_item, %options) = @_;
  1516.  
  1517.   $var = ref $var ? $var : rvar $var;
  1518.  
  1519.   my $res = $var->traverse_recursively
  1520.     ($fun_item,
  1521.      # The code that define the variable holding the result
  1522.      # of the recursive transformation of a subvariable.
  1523.      sub {
  1524.        my ($subvar, $parent_cond, @allresults) = @_;
  1525.        # Find a name for the variable, unless this is the top-variable
  1526.        # for which we want to use $resvar.
  1527.        my $varname =
  1528.      ($var != $subvar) ? _gen_varname ($base, @allresults) : $resvar;
  1529.        # Define the variable if required.
  1530.        unless ($nodefine)
  1531.      {
  1532.        # If the new variable is the source variable, we assume
  1533.        # we are trying to override a user variable.  Delete
  1534.        # the old variable first.
  1535.        variable_delete ($varname) if $varname eq $var->name;
  1536.        # Define an empty variable in condition TRUE if there is no
  1537.        # result.
  1538.        @allresults = ([TRUE, '']) unless @allresults;
  1539.        # Define the rewritten variable in all conditions not
  1540.        # already covered by user definitions.
  1541.        foreach my $pair (@allresults)
  1542.          {
  1543.            my ($cond, @result) = @$pair;
  1544.            my $var = var $varname;
  1545.            my @conds = ($var
  1546.                 ? $var->not_always_defined_in_cond ($cond)->conds
  1547.                 : $cond);
  1548.  
  1549.            foreach (@conds)
  1550.          {
  1551.            define ($varname, VAR_AUTOMAKE, '', $_, "@result",
  1552.                '', $where, VAR_PRETTY);
  1553.          }
  1554.          }
  1555.        set_seen $varname;
  1556.      }
  1557.        return "\$($varname)";
  1558.      },
  1559.      %options);
  1560.   return $res;
  1561. }
  1562.  
  1563.  
  1564. =back
  1565.  
  1566. =head1 SEE ALSO
  1567.  
  1568. L<Automake::VarDef>, L<Automake::Condition>,
  1569. L<Automake::DisjConditions>, L<Automake::Location>.
  1570.  
  1571. =cut
  1572.  
  1573. 1;
  1574.  
  1575. ### Setup "GNU" style for perl-mode and cperl-mode.
  1576. ## Local Variables:
  1577. ## perl-indent-level: 2
  1578. ## perl-continued-statement-offset: 2
  1579. ## perl-continued-brace-offset: 0
  1580. ## perl-brace-offset: 0
  1581. ## perl-brace-imaginary-offset: 0
  1582. ## perl-label-offset: -2
  1583. ## cperl-indent-level: 2
  1584. ## cperl-brace-offset: 0
  1585. ## cperl-continued-brace-offset: 0
  1586. ## cperl-label-offset: -2
  1587. ## cperl-extra-newline-before-brace: t
  1588. ## cperl-merge-trailing-else: nil
  1589. ## cperl-continued-statement-offset: 2
  1590. ## End:
  1591.